import { resolve } from 'node:path';
import fs from 'fs';
import path from 'path';

export function clearCompiledFile() {
  let folderPath = resolve('./lib');
  clear(folderPath);
  if (fs.existsSync('./index.d.ts')) fs.unlinkSync('./index.d.ts');
  if (fs.existsSync('./index.js')) fs.unlinkSync('./index.js');
}

function clear(directory: string) {
  let files = fs.readdirSync(directory);
  for (let file of files) {
    let filePath = path.join(directory, file);
    let stats = fs.statSync(filePath);
    if (stats.isDirectory()) {
      clear(filePath);
    } else {
      let needDelete = file.endsWith('.js') || file.endsWith('.d.ts');
      if (needDelete) fs.unlinkSync(filePath);
    }
  }
}

clearCompiledFile();
